package com.naiterui.ehp.bs.b2c.product.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bs.b2c.product.entity.Category;
import com.naiterui.ehp.bs.b2c.product.repository.ICategoryRepository;
import com.naiterui.ehp.bs.b2c.product.service.ICategoryService;
import com.naiterui.ehp.bs.b2c.product.vo.CategoryVO;

/**
 * <pre>
 * 商品分类相关service实现类
 * </pre>
 *
 * @author fangguanhong
 * @date 2019/5/22 下午2:54
 * @since 1.0.0
 */
@Service
public class CategoryServiceImpl implements ICategoryService {

    private final String imgUrl = ConfigUtil.getString("ehp.img.url");

    @Autowired
    private ICategoryRepository categoryRepository;

    /**
     * 分类树结构数据
     *
     * @return
     */
    @Override
    public List<CategoryVO> getCategoryList() {
        List<CategoryVO> result = new ArrayList<>();
        // 一级分类
        List<Category> primaryCategory = this.categoryRepository.findPrimaryCategory();
        // 二级分类
        List<Category> secondCategory = this.categoryRepository.findSecondCategory();
        // 一级ID--二级分类关系
        Map<Long, List<CategoryVO>> map = new HashMap<>();
        // 二级分类分组
        Optional.ofNullable(secondCategory).ifPresent(lis -> lis.forEach(category -> {
            List<CategoryVO> list = null;
            if (map.containsKey(category.getParentId())) {
                list = map.get(category.getParentId());
            } else {
                list = new ArrayList<>();
            }
            CategoryVO categoryVO = CategoryVO.builder()
                                              .id(category.getId())
                                              .parentId(category.getParentId())
                                              .name(category.getName())
                                              .description(category.getDescription())
                                              .icon(!StringUtils.isEmpty(category.getIcon()) ? this.imgUrl + category.getIcon() : null)
                                              .build();
            list.add(categoryVO);
            map.put(category.getParentId(), list);
        }));
        // 一级分类
        Optional.ofNullable(primaryCategory).ifPresent(lis -> lis.forEach(category -> {
            CategoryVO categoryVO = CategoryVO.builder()
                                              .id(category.getId())
                                              .parentId(category.getParentId())
                                              .name(category.getName())
                                              .childs(map.get(category.getId()))
                                              .description(category.getDescription())
                                              .icon(!StringUtils.isEmpty(category.getIcon()) ? this.imgUrl + category.getIcon() : null)
                                              .build();
            result.add(categoryVO);
        }));
        return result;
    }
}
